<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/kendocommon.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>辅导员客户信息</title>
    <meta charset="utf-8">
    <style>
    	table{font-size:12px}
    </style>
    <script>
    var admission_way=new Object();
        $(document).ready(function() {
        	
        	$(".various").fancybox({
        		maxWidth	: 1200,
        		maxHeight	: 600,
        		fitToView	: false,
        		width		: '70%',
        		height		: '70%',
        		autoSize	: false,
        		closeClick	: false,
        		openEffect	: 'none',
        		closeEffect	: 'none'
        	});
        	        
        	$("#toolbar").kendoToolBar({
	            resizable: false,
	            items: [
	            	{ type: "separator" },
	                //{ template: toolbarTemp},
					//{ type: "separator" },
	                { template: '<label>客户手机号:</label><input id="phoneNo" class="k-textbox"/>'},
	                
	        //    	{ template: '<label>入会来源</label><input id="admission_way" />'},
	        //        { template: '<label>孩子生日</label><input id="birthday"/>'},
	                
	                { type: "separator" },
	                { type: "button", text: "查询",click: queryCustomerList }
	           /*     { type: "separator" },
	                { type: "button", text: "添加为自己客户",click: saveAssistCustomer },
	                { type: "separator" },
	                { type: "button", text: "会员录入",click: registerPop }*/
	            ]
	        });

	        //客户列表数据源
            customerDS = new kendo.data.DataSource({
                transport: {
                    read:  {
                        url: "${path}/assist/querySsoUserInfo",
                        dataType: "json",
                        data: function() {
					        return {
					         	phone:$("#phoneNo").val()
					        };
					    }
                    }
                },
                //group: [{field: "businessTypeCode"}],
                serverPaging: true,
                serverFiltering: true,
                pageSize: 15,
                schema: {
                	data:"data",
               		total:"total"
                }
            });
            
            //辅导员客户列表数据源
            assistcustomerDS = new kendo.data.DataSource({
                transport: {
                    read:  {
                        url: "${path}/assist/queryAssistUserInfo",
                        dataType: "json",
                        data: function() {
					        return {
					         	phone:$("#phoneNo").val(),
					         	birthday:$("#birthday").val(),
					         	admission_way_code:$("#admission_way").val()
					        };
					    }
                    }
                },
                //group: [{field: "businessTypeCode"}],
                serverPaging: true,
                serverFiltering: true,
                pageSize: 15,
                schema: {
                	data:"data",
               		total:"total"
                }
            });
            
            //客户列表
   /*         $("#customergrid").kendoGrid({
            	toolbar: kendo.template("<font style='color:red;font-weight:bold'>公海客户</font>"),
                dataSource: customerDS,
                height:300,
                pageable: {
					refresh: true,
					pageSizes: true,
					buttonCount: 5,
					page: 1,
					pageSize: 10,
					pageSizes: [10, 20, 30],
					messages: {
						display: "显示 {0}-{1} 共 {2} 项",
						empty: "没有数据",
						itemsPerPage: "每面显示数量",
						first: "第一页",
						last: "最后一页",
						next: "下一页",
						previous: "上一页"
					}
				},
                selectable: "multiple row",
                allowCopy: true,
                //editable: 'incell',
                columns: [
                	{ field:"checked", template: "<input type='checkbox' class='gridchk'/>",width: "120px",
                		headerTemplate: '<input type="checkbox" id="check-all"/>',editable: false},
                    { field:"real_name", title: "客户姓名",width: "250px",editable: false},
                    //{ field:"phone", title: "客户手机号",width: "150px",editable: false},
                    { field:"member_name", title: "家庭称谓",width: "250px",editable: false},
                    { field:"sex", title: "性别",width: "250px"},
                    { field:"admission_note", title: "入会备注",width: "250px"},
                    //{ field: "UnitsInStock", title:"Units In Stock", width:"120px"},//,format:"{0:yyyy/MM/dd}"
                    //{ command: [{name:"editrow",text:"<span class='k-icon k-edit'></span>查看",click:queryCustomerInfo,buttonType:"ImageAndText"}], title: "操作", width: "100px" }
                ]
            });*/
            
            
            //辅导员客户列表
            $("#assistcustomergrid").kendoGrid({
            	toolbar: kendo.template("<font style='color:red;font-weight:bold'>我的客户</font>"),
                dataSource: assistcustomerDS,
                pageable: {
					refresh: true,
					pageSizes: true,
					buttonCount: 5,
					page: 1,
					pageSize: 10,
					pageSizes: [10, 20, 30],
					messages: {
						display: "显示 {0}-{1} 共 {2} 项",
						empty: "没有数据",
						itemsPerPage: "每面显示数量",
						first: "第一页",
						last: "最后一页",
						next: "下一页",
						previous: "上一页"
					}
				},
                selectable: "multiple row",
                allowCopy: true,
                //editable: 'incell',
                columns: [
                    { field:"username", title: "客户姓名",width: "120px",editable: false,template:queryCustomer},
                    { field:"phone", title: "客户手机号",width: "80px",editable: false},
                    { field:"note", title: "添加备注",width: "200px",editable: false},
                    { field:"createdtime", title: "添加时间",width: "120px"},
                    //{ field: "UnitsInStock", title:"Units In Stock", width:"120px"},//,format:"{0:yyyy/MM/dd}"
                    { command: [{name:"deleterow",text:"<span class='k-icon k-delete'></span>删除",click:deleteAssistCus,buttonType:"ImageAndText"},
                                {name:"listrow",text:"<span class='k-icon k-i-search'></span>客户访问记录",click:crmServicePop,buttonType:"ImageAndText"},
                                {name:"rechargeHis",text:"充值记录",click:rechargeHis},
                                {name:"consumeHis",text:"消费记录",click:consumeHis},
                                {name:"membercardHis",text:"零售记录",click:membercardHis}
                                ], title: "操作", width: "180px" }
                ]
            });
            
    		$("#birthday").kendoDatePicker({
    			format: "yyyy-MM-dd",
    			  animation: false
    			});
    	
    		
    		var admissionDataSource = new kendo.data.DataSource({
                transport:{
                    read:{
                        url: "${path}/common/getBaseCodeList?type=admission_way",
                        dataType: "json"
                    }
                }
            });
            
    		$("#admission_way").kendoComboBox({
    	     	dataValueField: "code",
    	        dataTextField: "name",
    	        dataSource: admissionDataSource,
    	        select: function(e) {
		        	//debugger;
					var value = this.value();
    				var item = e.item;  							      					    
    				admission_way=e.sender.dataItem(item);
				}   	        
    		});
                
            
            
            $('#check-all').change(function(){
            	var customergrid = $("#customergrid").data("kendoGrid");
            	$.each(customergrid.items(), function (i, item) {
					var cbxController = $(item).find(".gridchk");
					var isChecked = cbxController.is(":checked");
					if(isChecked){
						cbxController.prop("checked",false); 
					}else{
						cbxController.prop("checked",true); 
					}
				});
            });
            
            $('#assistcustomergrid').css("height",document.body.clientHeight-340);
            
		});
        
     	//查询客户信息
  		var queryCustomer = function(dataItem){
  			return "<a href='${path}/assist/memberInfo?c_user_id="+dataItem.cuserid+"' style='color:#000' class='various' data-fancybox-type='iframe' title='会员信息'>" + dataItem.username + "</a>";
  		};
		
		//辅导员删除绑定关系
		var deleteAssistCus = function(e){
			layer.confirm('确定删除?', {icon: 3, title:'提示'}, function(index){
				var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
				e.preventDefault();
	            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));
	            if(data.cuserid){
	            	$.ajax({
				        url: "${path}/assist/delAssistUserInfo",
				        dataType: 'json',
		   				//contentType: "application/json",
				        data: {c_user_id:data.cuserid},
				        type:"POST", 
				        success: function (text) {
				        	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
				        	toastr.info(text.msg, "提示", toastrOptsBottom);
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
				        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
				        }
				    });
	            }
			  	layer.close(index);
			});
		}
		
		var queryCustomerList = function(e){//查询
		//	var customergrid = $("#customergrid").data("kendoGrid");
		//	$("#customergrid").data("kendoGrid").dataSource.fetch();
			
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
			$("#assistcustomergrid").data("kendoGrid").dataSource.page(1);
		}
		
		//保存
		var saveAssistCustomer = function(e){
			var customergrid = $("#customergrid").data("kendoGrid");
			if(customergrid.items().length==0){
				return toastr.info("请先选择用户", "提示", toastrOptsBottom);
			}
			var checkedArr = new Array();
			checkedArr.push({parkid:$('#park').val()});
			//var model = {};
			var note;
			layer.prompt({
           	    formType: 2,
           	    //value: '',
           	    title: '分配客户'
           	}, function(value, index, elem){
           		note = value;
           		$.each(customergrid.items(), function (i, item) {
    				var cbxController = $(item).find(".gridchk");
    				var isChecked = cbxController.is(":checked");
    				var curData = customergrid.dataItem(item);
    				if (isChecked){
    					//model.id = curData.id;
    					//model.parkPrice = curData.parkPrice;
    					//checkedArr.push({id:curData.id,parkPrice:curData.parkPrice});
    					curData.note = note;
    					checkedArr.push(curData);
    				}
    			});
    			
    			$.ajax({
    		        url: "${path}/assist/saveAssistUserInfo",
    		        dataType: 'json',
       				contentType: "application/json",
    		        data: JSON.stringify(checkedArr),
    		        type:"POST", 
    		        success: function (text) {
    		        	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
    		        	toastr.info(text.msg, "提示", toastrOptsBottom);
    		        },
    		        error: function (jqXHR, textStatus, errorThrown) {
    		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
    		        }
    		    });
    			layer.close(index);
           	});
		}
		
		//用户注册
		var layIndex;
		var registerPop = function(e){
			layIndex = layer.open({
				title:"客户录入",
				type: 2,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['850px', '600px'], //宽高
			    content: "${path}/business/addUser",
			    cancel: function(index){
			    	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
		        }
			});
			//$.fancybox($("#register"));
		}
		
		var crmServicePop = function(e){
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			e.preventDefault();
            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));
            if(data.cuserid==null||data.cuserid==""){
            	return layer.msg("客户编号不能为空", {time: 3000, icon:6});
            }
			layIndex = layer.open({
				title:"客户服务记录",
				type: 2,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['900px', '650px'], //宽高
			    content: "http://oa.dibabo.cn/ShopAdmin/isp/isp_entity/crm_service_log/listpage/crm_service_log_ListPage?cuserid="+data.cuserid
			});
			//$.fancybox($("#register"));
		}
		
		var rechargeHis = function(e){
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			e.preventDefault();
            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));

			layIndex = layer.open({
				title:"充值记录",
				type: 2,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['900px', '650px'], //宽高
			    content: "http://oatest.dibabo.cn/ShopAdmin/isp/isp_entity/order_detail/ListPage/order_detail_ListPageForRecharge?userId="+data.cuserid
			});
			//$.fancybox($("#register"));
		}
		
		var consumeHis = function(e){
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			e.preventDefault();
            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));

			layIndex = layer.open({
				title:"消费记录",
				type: 2,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['900px', '650px'], //宽高
			    content: "http://oatest.dibabo.cn/ShopAdmin/isp/isp_entity/order_detail/ListPage/order_detail_ListPageForShopping?userId="+data.cuserid
			});
			//$.fancybox($("#register"));
		}
		
		var membercardHis = function(e){
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			e.preventDefault();
            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));

			layIndex = layer.open({
				title:"零售消费记录",
				type: 2,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['900px', '650px'], //宽高
			    content: "http://oatest.dibabo.cn/ShopAdmin/isp/isp_entity/contract/ListPage/contract_ListPageForRetail?userId="+data.cuserid
			});
			//$.fancybox($("#register"));
		}
		
		
    </script>
</head>
<body>
	<div id="toolbar" style="margin:2px"></div>
   <!--  <div id="customergrid" style="margin:2px;font-size:10px"></div>--> 
    <div id="assistcustomergrid" style="margin:2px;font-size:10px"></div>
    
    <div style="display: none;">		
		<div id="inlineForm" style="width:700px;overflow:auto;">
			<table>
                <tr>
                    <td style="width:100px;padding:5px">
                        <span  style="width:90px">内容</span>
                    </td>
                    <td style="padding:5px">
                        <textarea name="note" id="note" style="width:495px;height:200px" class="k-textbox"></textarea>
                    </td>
                </tr>
            </table>
		</div>
	</div>
	
	
	<a href="${path}/business/addUser" class="various" data-fancybox-type="iframe" id="register" title="客户录入" style="display:none"></a>
	<!-- assist/registerbyassist -->
</body>
</html>
